// pages/posts/post-detail/post-detail.js
var postDatas = require("../../../data/posts_data.js");
Page({
  data: {},
  onLoad: function(option) {
    var postId = option.id;
    // this.data.currentPostId = postId;
    this.setData({
      currentPostId: postId
    })
    var postData = postDatas.postList[postId]

    this.setData({
      postData: postData
    });
    //缓存
    // wx.setStorageSync('key', '风暴英雄');
    //修改缓存 重新复制
    // var testObj = {game: '风暴英雄',developer: "暴雪"}
    // wx.setStorageSync('key', testObj);

    var postsCollectde = wx.getStorageSync('posts_collectde');
    if (postsCollectde) {

      var collected = postsCollectde[postId];
      this.setData({
        collected: collected
      })
    } else {

      var postsCollectde = {};
      postsCollectde[postId] = false;
      wx.setStorageSync('posts_collectde', postsCollectde);
    }


  },
  onCollectionTap: function(event) {
    //同步 获取缓存
    // var game = wx.getStorageSync('key');
    // console.log(game)
    var postsCollectde = wx.getStorageSync('posts_collectde');
    var postCollectde = postsCollectde[this.data.currentPostId];

    postCollectde = !postCollectde
    postsCollectde[this.data.currentPostId] = postCollectde
    // this.showModal(postsCollectde, postCollectde);
    this.showToast(postsCollectde, postCollectde);
  },
  onShareTap: function(event) { //分享
    // 微信缓存不能超过10M
    // 清除指定缓存
    // wx.removeStorageSync('key')
    // 清除所有缓存
    // wx.clearStorageSync(); 
    console.log(this.data.backgroundAudioManager.pause())
    // wx.showActionSheet({
    //   itemList: ['A', 'B', 'C'],
    //   success(res) {
    //     console.log(1111)
    //     console.log(res.tapIndex)
    //   },
    //   fail(res) {
    //     console.log(222)
    //     console.log(res.errMsg)
    //   }
    // })



  },
  showModal(postsCollectde, postCollectde) {
    var that = this;
    wx.showModal({
      title: '收藏',
      content: postCollectde ? "收藏该文章?" : "取消收藏该文章? ",
      showCancel: true,
      cancelText: '我是取消',
      confirmText: '我是确定',
      success: function(res) {
        if (res.confirm) {
          wx.setStorageSync('posts_collectde', postsCollectde)
          that.setData({
            collected: postCollectde
          })

        }
      }
    })
  },

  showToast(postsCollectde, postCollectde) {
    var that = this;
    wx.setStorageSync('posts_collectde', postsCollectde)
    that.setData({
      collected: postCollectde
    });

    wx.showToast({
      title: postCollectde ? "收藏成功" : "取消成功",
      duration: 1000, //提示的延迟时间
      icon: 'success'
    });
  },
  onMusticTap: function(event) {
    //初始化 
    var isPlayingMusci = this.data.backgroundAudioManager;
    if (isPlayingMusci) {
      if (this.data.isPlayingMusci == true) {
        isPlayingMusci.pause();
        this.data.isPlayingMusci = false
      } else {
        isPlayingMusci.play()
        this.data.isPlayingMusci = true
      }
    } else {
      this.initMusic()
      this.setData({
        isPlayingMusci: true
      })
    }
    // if (isPlayingMusci) {
    //   setTimeout(function() {
    //     isPlayingMusci.pause();
    //   }, 3000);
    // }
  },
  initMusic: function() {
    const backgroundAudioManager = wx.getBackgroundAudioManager();
    this.data.backgroundAudioManager = backgroundAudioManager;
    backgroundAudioManager.title = '此时此刻'
    backgroundAudioManager.epname = '此时此刻'
    backgroundAudioManager.singer = '许巍'
    backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
    // 设置了 src 之后会自动播放
    backgroundAudioManager.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
  }
})